package com.party.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.party.business.domain.Kc;
import com.party.business.service.IKcService;
import com.party.business.service.ITLbImageService;
import com.party.common.annotation.Log;
import com.party.common.config.RuoYiConfig;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 课程信息Controller
 *
 */
@RestController
@RequestMapping("/business/kcxx" )
public class KcController extends BaseController {
    @Autowired
    private IKcService kcService;
    @Autowired
    private ITLbImageService tLbImageService;

/**
 * 查询课程信息列表
 */
//@RequiresPermissions("system:kc:list")
@GetMapping("/list" )
    public TableDataInfo list(Kc kc) {
        startPage();
        List<Kc> list = kcService.selectKcList(kc);
        return getDataTable(list);
    }




    /**
     * 导出课程信息列表
     */
    //@RequiresPermissions("system:kc:export")
    @Log(title = "课程信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, Kc kc) {
        List<Kc> list = kcService.selectKcList(kc);
        ExcelUtil<Kc> util = new ExcelUtil<Kc>(Kc. class);
        util.exportExcel(response, list, "课程信息数据" );
    }

    /**
     * 获取课程信息详细信息
     */
    //@RequiresPermissions("system:kc:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(kcService.getById(id));
    }

    /**
     * 新增课程信息
     */
    //@RequiresPermissions("system:kc:add")
    @Log(title = "课程信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody Kc kc) {
        return toAjax(kcService.save(kc));
    }

    /**
     * 修改课程信息
     */
    //@RequiresPermissions("system:kc:edit")
    @Log(title = "课程信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated  @RequestBody Kc kc) {
        return toAjax(kcService.updateById(kc));
    }

    /**
     * 删除课程信息
     */
    //@RequiresPermissions("system:kc:remove")
    @Log(title = "课程信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(kcService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 获取海报图片
     */
    @Log(title = "获取海报图片" , businessType = BusinessType.OTHER)
    @GetMapping("/findPosterImage")
    public AjaxResult findPosterImage(Kc  kc) {
        String type = "1";
        Kc byId = kcService.getById(kc.getId());
        if(byId==null){
            return AjaxResult.error("课程不存在");
        }
        String imageByType = tLbImageService.findImageByType(type,byId.getTitle());
        return AjaxResult.success(imageByType);
    }
}
